const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');

module.exports = {
    entry: './src/main.js',  // 入口文件
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, './dist/'),
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './public/index.html',
        }),
        new CleanWebpackPlugin(),
        new VueLoaderPlugin(),
    ],
    devServer: {
        port: 8001, // 端口
        compress: true, //  gzip 压缩
        hot: true, // 热重载
        inline: true, // 内联模式 通过内置脚本监听热重载
        open: true, // 打开浏览器
    },
    resolve: {
        alias: {
            '@': path.resolve('./src'),
        },
        extensions: ['.js', '.vue']  // 捕捉匹配文件
    },
    module: {
        rules: [
            {
                // 处理css
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                //  处理图片
                test: /\.(jpg|png|svg|gif)$/,
                use: [
                    'file-loader'
                ]
            },
            {
                // 字体
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: [
                    'file-loader'
                ]
            },
            {
                // 用来兼容 babel 模块和插件,用来转换为向后兼容的JavaScript语法
                test: /\.js$/,
                use: [
                    'babel-loader'
                ],
                exclude: /node_modules/
            },
            {
                // 处理vue
                test: /\.vue$/,
                use: [
                    'vue-loader'
                ],
            },
            {
                // sass
                test: /\.scss$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'sass-loader'
                ]
            },
        ]
    }
};
